import Vue from 'vue'
import Vuex from 'vuex'
import createLogger from 'vuex/dist/logger'
import createPersistedState from "vuex-persistedstate";
import { shop_fav, shop_favlist, shop_unfav, shopcart_list, shopcart_clear, shopcart_add, shopcart_sub } from "../utils/api";

Vue.use(Vuex)

export default new Vuex.Store({
        state: {
                // collectlist: localStorage.getItem('vuex') ? JSON.parse(localStorage.getItem('vuex')).collectlist : [],
                collectlist: [],
                shopcartlist: [],
        },
        getters: {
        },
        mutations: {
                collect (state, payload) {
                        state.collectlist.push(payload);
                        console.log(state.collectlist);
                        // localStorage.setItem('vuex', JSON.stringify(state));
                },
                cancel_collect (state, payload) { //取消收藏一个店铺
                        var index = state.collectlist.findIndex((item) => {
                                return item.mtWmPoiId == payload.mtWmPoiId;
                        })
                        if (index != -1) {
                                state.collectlist.splice(index, 1);
                        }
                        console.log(state.collectlist);
                        // localStorage.setItem('vuex', JSON.stringify(state));
                },
                save_favlist (state, payload) {
                        state.collectlist = payload;
                },
                save_shopcartlist (state, payload) {
                        state.shopcartlist = payload;
                },
                clear (state) {
                        state.shopcartlist = []
                },
                shopcart_add (state, payload) {
                        var index = state.shopcartlist.findIndex((item) => {
                                return item.id == payload.id;
                        })
                        if (index != -1) {
                                state.shopcartlist[index].count++;
                        } else {
                                var good = JSON.parse(JSON.stringify(payload))
                                good.count = 1;
                                state.shopcartlist.push(good)
                        }
                },
                shopcart_sub (state, payload) {
                        var index = state.shopcartlist.findIndex((item) => {
                                return item.id == payload.id;
                        })
                        if (index != -1) {
                                state.shopcartlist[index].count--;
                        } else {
                                var good = JSON.parse(JSON.stringify(payload))
                                good.count = 1;
                                state.shopcartlist.push(good)
                        }
                }
        },
        actions: {
                request_favlist (store) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shop_favlist({ userid }).then((ok) => {
                                console.log(ok.data.list)
                                store.commit('save_favlist', ok.data.list)
                        })
                },
                request_fav (store, payload) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shop_fav({ userid, shopid: payload.mtWmPoiId }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('collect', payload)
                                }
                        })
                },
                request_unfav (store, payload) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shop_unfav({ userid, shopid: payload.mtWmPoiId }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('cancel_collect', payload)
                                }
                        })
                },
                request_shopcartlist (store) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shopcart_list({ userid }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('save_shopcartlist', ok.data.code)
                                }
                        })
                },
                request_clear (store) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shopcart_clear({ userid }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('clear');
                                }
                        })
                },
                request_shopcart_add (store, payload) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shopcart_add({ userid, goodid: payload.id }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('shopcart_add', payload);
                                }
                        })
                },
                request_shopcart_sub (store, payload) {
                        var userid = JSON.parse(localStorage.getItem('userinfo')).id;
                        shopcart_sub({ userid, goodid: payload.id }).then((ok) => {
                                if (ok.data.code == 200) {
                                        store.commit('shopcart_sub', payload);
                                }
                        })
                }

        },
        modules: {
        },
        //配合其他插件 管理vuex中state内部的数据
        plugins: [createLogger(), createPersistedState()]

})
